In allocating memory for a computer program, older programming languages require that the size of an array or data item be declared before the program is compiled. Moreover, the size of the array or data item could not be exceeded during run-time unless the program was changed and recompiled. Today, however, most modern programming languages, including C and C++, allow the user to request memory blocks from the system memory at run-time and release the memory blocks back to the system memory when the program no longer needs the memory blocks.
The C programming language provides memory management capability with a set of library functions known as “storage management” routines. The most basic memory allocation function is called malloc which allocates a requested number of bytes of memory and returns a pointer to the starting address of the memory allocated. Another function known as free returns the memory previously allocated by malloc so that it can be allocated again for use by other routines.
For applications in which memory allocation occurs in parallel and asynchronously, for example, in a multi-threaded process, the malloc and free storage management functions must be properly managed to avoid corruption of the memory resource. Corruption may occur where one thread is modifying a global structure when another thread is trying to read it. The code that implements the threads in the multi-threaded process has to be “code-locked” so that at any one time, only a single thread executes a storage management function to completion before another thread is allowed to execute a similar function. Code-locking is typically implemented using a global locking mechanism, such as a mutex. When one thread calls any of the memory management routines (e.g., malloc, free, realloc), it locks the global locking mechanism so that the other threads are prevented from executing any memory management routine until the memory management routine called by the first thread has completed. The other threads will have to wait until the first thread releases the global locking mechanism. The locking mechanism therefore effectively serializes access to the memory. Thus, in a multi-threaded process in which memory allocation functions are used extensively, the need for such a locking mechanism seriously compromises the speed of the system. U.S. Pat. No. 6,058,460 is an example of a multi-threaded system that uses a locking mechanism for memory management.
Such a generic locking mechanism though necessary in some multi-threaded applications is not always essential or ideal in other multi-threaded applications.